package com.ruoyi.web.controller.wxqy;

import com.ruoyi.common.annotation.ControllerAuthority;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.cache.Meta;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.qywx.service.inters.SyncQiyeService;
import com.ruoyi.system.service.ISysTeamService;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author : hcl
 * @Date : 2023/8/8
 * 同步企业微信数据信息
 * 部门、成员配置、标签
 */
@RestController
@RequestMapping("/wxqy/sync")
@ControllerAuthority
public class UpdateQyController extends BaseController {
    @Autowired
    private ISysTeamService teamService;
    @Autowired
    private SyncQiyeService syncQiyeService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 同步企业微信部门数据
     */
    @PostMapping("/dept-user")
    public AjaxResult syncQiyeTxl() {
        String key = CacheConstants.QYWX_SYNC_UPDATE + "SYNC_CONTACT_" + getTeamId();
        Boolean sync = (Boolean) redisCache.getCacheObject(key);
        if (sync != null && sync) {
            return error("正在同步中，请勿频繁调用");
        }
        Meta meta = teamService.selectTeamMeta(getTeamId());
        if (Objects.isNull(meta)) return error("获取项目信息失败！");
        syncQiyeService.syncAppTxl(meta);
        redisCache.setCacheObject(key, true, 30, TimeUnit.MINUTES);
        return success();
    }

    @PostMapping("/tags")
    public AjaxResult syncQywxTags(){
        String key = CacheConstants.QYWX_SYNC_UPDATE + "SYNC_TAG_" + getTeamId();
//        Boolean sync = (Boolean) redisCache.getCacheObject(key);
//        if (sync != null && sync) {
//            return error("正在同步中，请勿频繁调用");
//        }
        Meta meta = teamService.selectTeamMeta(getTeamId());
        if (Objects.isNull(meta)) return error("获取项目信息失败！");
        syncQiyeService.syncQiyeTags(meta);
        redisCache.setCacheObject(key, true, 30, TimeUnit.MINUTES);
        return success();
    }
}
